package com.fled.itsmbigscreen.impl;

import com.alibaba.fastjson.JSONObject;
import com.fled.itsmbigscreen.config.SmallSever;
import com.fled.itsmbigscreen.config.WebSocketSever;
import com.fled.itsmbigscreen.enums.AlarmStatusEnums;
import com.fled.itsmbigscreen.mapper.DeviceAlarmMapper;
import com.fled.itsmbigscreen.mapper.HuadongPduMapper;
import com.fled.itsmbigscreen.pojo.DeviceAlarm;
import com.fled.itsmbigscreen.pojo.DeviceAlarmExample;
import com.fled.itsmbigscreen.pojo.HuadongPdu;
import com.fled.itsmbigscreen.pojo.HuadongPduExample;
import com.fled.itsmbigscreen.service.PduService;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.*;

@Service
public class AlarmListener {
    @Autowired
    private DeviceAlarmMapper deviceAlarmMapper;
    @Autowired
    private HuadongPduMapper huadongPduMapper;
    @Autowired
    private PduService pduService;

//    @RabbitListener(queuesToDeclare = @Queue(value = "pduAlarmQueue"))
//    public void listener(String msg) {
//        //存入告警表
//        DeviceAlarm deviceAlarm = JSONObject.parseObject(msg, DeviceAlarm.class);
//        if (deviceAlarm.getOperationStatus().equals(1)) {
//            DeviceAlarmExample deviceAlarmExample = new DeviceAlarmExample();
//            deviceAlarmExample.createCriteria().andOperationStatusEqualTo(0).andFromIdEqualTo(deviceAlarm.getFromId()).andTypeEqualTo(deviceAlarm.getType());
//            List<DeviceAlarm> deviceAlarms = deviceAlarmMapper.selectByExample(deviceAlarmExample);
//            for (DeviceAlarm alarm : deviceAlarms) {
//                alarm.setOperationStatus(1);
//                deviceAlarmMapper.updateByPrimaryKeySelective(alarm);
//            }
//        } else {
//            System.out.println("123333333333333333333333333333333333333333333333");
//            deviceAlarmMapper.insertSelective(deviceAlarm);
//            //发送邮箱
//            try {
//                pduService.sendMail(AlarmStatusEnums.statOf(deviceAlarm.getType()).getEmsg(), deviceAlarm.getFromId()+"号探测器："+deviceAlarm.getContent());
//            } catch (MessagingException e) {
//                e.printStackTrace();
//            }
//        }
//        //发送webstock
//        DeviceAlarmExample deviceAlarmExample = new DeviceAlarmExample();
//        deviceAlarmExample.createCriteria().andOperationStatusEqualTo(0);
//        List<DeviceAlarm> deviceAlarms = deviceAlarmMapper.selectByExample(deviceAlarmExample);
//        if (deviceAlarms.size() > 0) {
//
//        } else {
//            deviceAlarms = new ArrayList<>();
////            WebSocketSever.sendMessageByUser(1, new );
//        }
//        HuadongPduExample huadongPduExample = new HuadongPduExample();
//        huadongPduExample.createCriteria().andPduIdEqualTo(deviceAlarm.getFromId().toString());
//        List<HuadongPdu> huadongPdus = huadongPduMapper.selectByExample(huadongPduExample);
//        if (huadongPdus != null && huadongPdus.size() > 0) {
//            deviceAlarms.stream().forEach(deviceAlarm1 -> deviceAlarm1.setRoomId(huadongPdus.get(0).getRoomId()));
//            try {
//                WebSocketSever.sendAllMessage(JSONObject.toJSONString(deviceAlarms));
//                SmallSever.sendMessageByUser(huadongPdus.get(0).getRoomId(), JSONObject.toJSONString(deviceAlarms));
//
//            } catch (Exception exception) {
//                exception.printStackTrace();
//            }
//        }
//
//
//        System.out.println(msg);
//    }


    public static void main(String[] args) {
        List<DeviceAlarm> deviceAlarms = new ArrayList<>();
        String s = JSONObject.toJSONString(deviceAlarms);
        System.out.println(s);
    }
}
